package screen.component
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.text.TextField;
	
	import helper.Services;
	import helper.ValueObject;
	
	import screen.TechComBankScreen;

	public class Rank extends Popup
	{
		private var _tech_com_bank_screen:TechComBankScreen;
		private var _current_no:Number = 0;
		private const _PAGE_NO:Number = 7;
		private var _data_arr:Array=[];
		public function Rank()
		{
			init();
		}
		private function init():void{
			_tech_com_bank_screen = TechComBankScreen.getInstance();
			_popup = ValueObject.T1_RESOURCE.getExportedAsset("rankPopup") as MovieClip;
			buttonImageInit(_popup.preBtn);
			MovieClip(_popup.preBtn).addEventListener(MouseEvent.CLICK,preClick);
			buttonImageInit(_popup.nextBtn);
			MovieClip(_popup.nextBtn).addEventListener(MouseEvent.CLICK,nextClick);
			popupInit(_popup);
		}
		private function getTopCompete(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getTopCompete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getTopError);
			var arr:Array =JSON.decode(e.target.data);
			_data_arr = arr;
			var leng:int = Math.min(_PAGE_NO,arr.length);
			_current_no =0;
			for(var i:int=0;i<leng;i++){
				var rank_unit:MovieClip = _popup.getChildByName("rank"+ i) as MovieClip;
				TextField(rank_unit.rankTf).text = String(i+1);
				TextField(rank_unit.addressTf).text = arr[i].address;
				TextField(rank_unit.nameTf).text =arr[i].username;
				TextField(rank_unit.scoreTf).text = arr[i].score;
			}
		}
		
		private function nextClick(e:MouseEvent):void{
			_current_no +=_PAGE_NO;
			var leng:int = Math.min(_current_no + _PAGE_NO,_data_arr.length);
			if(_current_no < leng){
				deleteRank();
			}else{
				_current_no -=_PAGE_NO;
				return;
			}
			checkButtonStatus()
			for(var i:Number = _current_no;i<leng;i++){
				var rank_unit:MovieClip = _popup.getChildByName("rank"+ (i%7)) as MovieClip;
				TextField(rank_unit.rankTf).text = String(i+1);
				TextField(rank_unit.addressTf).text = _data_arr[i].address;
				TextField(rank_unit.nameTf).text =_data_arr[i].username;
				TextField(rank_unit.scoreTf).text = _data_arr[i].score;
			}
		}
		private function preClick(e:MouseEvent):void{
			_current_no -=_PAGE_NO;
			var leng:int = Math.min(_current_no + _PAGE_NO,_data_arr.length);
			if(_current_no >= 0){
				deleteRank();
			}else{
				_current_no +=_PAGE_NO;
				return;
			}
			checkButtonStatus()
			for(var i:Number = _current_no;i<leng;i++){
				var rank_unit:MovieClip = _popup.getChildByName("rank"+ (i%7)) as MovieClip;
				TextField(rank_unit.rankTf).text = String(i+1);
				TextField(rank_unit.addressTf).text = _data_arr[i].address;
				TextField(rank_unit.nameTf).text =_data_arr[i].username;
				TextField(rank_unit.scoreTf).text = _data_arr[i].score;
			}
		}
		private function deleteRank():void{
			
			for(var i:int=0;i<_PAGE_NO;i++){
				var rank_unit:MovieClip = _popup.getChildByName("rank"+ i) as MovieClip;
				TextField(rank_unit.rankTf).text = 
					TextField(rank_unit.addressTf).text = 
					TextField(rank_unit.nameTf).text =
					TextField(rank_unit.scoreTf).text = "";
			}
		}
		private function checkButtonStatus():void{
			if(_current_no<7){
				_popup.preBtn.visible = false;
			}else{
				_popup.preBtn.visible = true;
			}
			if(_data_arr.length-_current_no<_PAGE_NO){
				_popup.nextBtn.visible = false;
			}else{
				_popup.nextBtn.visible = true;;
			}
		}
		private function getTopError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getTopCompete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getTopError);
		}
		override public  function show():void{
			super.show();
			Services.getInstance().getTop(getTopCompete,getTopError);
		}
	}
}